package cn.cmkj.auction.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

import cn.cmkj.auction.R;
import cn.cmkj.auction.data.http.Auction;
import cn.cmkj.auction.widget.ptr.recyclerview.BaseRecycleAdapter;

/**
 * Created by cunguoyao on 2017/9/24.
 */

public class AuctionListAdapter extends BaseRecycleAdapter {

    private Context context;
    private List<Auction> mData;
    private ImageOptions imageOptions;

    public AuctionListAdapter(Context context, List<Auction> mData) {
        this.context = context;
        this.mData = mData;
        this.imageOptions = new ImageOptions.Builder().setFadeIn(true)//淡入效果
                .setFailureDrawableId(R.drawable.empty_photo)
                .setImageScaleType(ImageView.ScaleType.FIT_XY).build();
    }

    @Override
    public int onSpan(int position) {
        return 2;
    }

    @Override
    public int getItemCount() {
        int size = mData != null ? mData.size() : 0;
        return size;
    }

    public Auction getItem(int position) {
        if(mData == null || mData.size() <= 0)return null;
        Auction item = mData.get(position);
        return item;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext ()).inflate(R.layout.adapter_list_auction, parent, false);
        return new AuctionViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Auction item = mData.get(position);
        if(item != null) {
            ((AuctionViewHolder) holder).paiGoodName.setText(item.getName());
            ((AuctionViewHolder) holder).paiGoodPrice.setText("起拍价：" + item.getStartPrice());
            if(item.getStatusStr().equals("")) {
                ((AuctionViewHolder) holder).paiState.setImageResource(R.drawable.pai_state_finish);
            }else {
                ((AuctionViewHolder) holder).paiState.setImageResource(R.drawable.pai_state_ing);
            }
            x.image().bind(((AuctionViewHolder) holder).paiGoodImage, item.getImageUrl(), imageOptions);
        }
    }

    class AuctionViewHolder extends RecyclerView.ViewHolder {
        public LinearLayout rootView;
        public ImageView paiGoodImage;
        public ImageView paiState;
        public TextView paiGoodName;
        public TextView paiGoodPrice;

        public AuctionViewHolder(View itemView) {
            super(itemView);
            this.rootView = (LinearLayout) itemView.findViewById(R.id.linear_layout);
            this.paiGoodImage = (ImageView) itemView.findViewById (R.id.pai_good_image);
            this.paiState = (ImageView) itemView.findViewById (R.id.pai_state);
            this.paiGoodName = (TextView) itemView.findViewById (R.id.pai_good_name);
            this.paiGoodPrice = (TextView) itemView.findViewById (R.id.pai_good_price);
        }
    }
}